package com.goldsprite.infinityworld.screens.examples.ui;

import com.badlogic.gdx.Gdx;
import com.badlogic.gdx.graphics.Color;
import com.badlogic.gdx.graphics.OrthographicCamera;
import com.badlogic.gdx.graphics.Texture;
import com.badlogic.gdx.graphics.g2d.Batch;
import com.badlogic.gdx.math.Vector2;
import com.badlogic.gdx.scenes.scene2d.Actor;
import com.badlogic.gdx.scenes.scene2d.Stage;
import com.badlogic.gdx.scenes.scene2d.ui.*;
import com.badlogic.gdx.scenes.scene2d.utils.ChangeListener;
import com.badlogic.gdx.utils.ScreenUtils;
import com.badlogic.gdx.utils.viewport.FitViewport;
import com.badlogic.gdx.utils.viewport.Viewport;
import com.goldsprite.gdxcore.logs.LogViewerService;
import com.goldsprite.gdxcore.screens.GScreen;
import com.goldsprite.gdxcore.ui.GSplitPane;
import com.goldsprite.gdxcore.utils.ColorTextureUtils;
import com.goldsprite.infinityworld.assets.GlobalAssets;

/**
 * 一个演示用于展示如何将游戏视图嵌入其他UI视图中, 例如编辑器视图
 * 首先创建一个舞台并添加ui布局: 一个分割栏左边是游戏视图, 右边是功能菜单
 * 游戏视图可以设置虚拟视口大小以及操作相机, 画面大小将以父布局分配区域左下为绘制起点, 右上为绘制终点.
 */
public class ChildViewportTest extends GScreen {
	private Skin skin;
	private Texture backTex;

	private OrthographicCamera editorCam;
	private FitViewport editorViewport;
	private float editorWidth = 960, editorHeight = 540;
	private Stage uiStage;

	@Override
	public Viewport getViewport() {
		return editorViewport;
	}


	@Override
	public void create() {
		skin = GlobalAssets.getInstance().editorSkin;
		backTex = ColorTextureUtils.createColorTexture(new Color(0.4f, 0.4f, 0.8f, 1f));

		editorCam = new OrthographicCamera();
		editorViewport = new FitViewport(editorWidth, editorHeight, editorCam);
		editorViewport.apply(true);


		uiStage = new Stage(editorViewport);
		getImp().addProcessor(uiStage);

		LogViewerService logViewer = new LogViewerService(skin);

		//游戏视图布局
		Table gameTable = new Table(skin);
		//游戏主布局
		InnerViewWidgetGroup gameMainLayout = new InnerViewWidgetGroup(skin, editorViewport);
		gameTable.add(gameMainLayout).expand().fill();

		//修改器布局
		Table modifierTable = new Table(skin);
		modifierTable.setBackground("list");
		modifierTable.top().left();
		modifierTable.setFillParent(true);

		//修改器标题
		TextButton modifierTitleBtn = new TextButton("Modifier", skin);
		modifierTable.add(modifierTitleBtn).expandX().fillX().row();

		//修改器内容布局
		{
			Table ajustChildViewportScl = new Table(skin);
			ajustChildViewportScl.left();
			ajustChildViewportScl.defaults().space(10);
			Label label = new Label("子视口拉伸(1.00)", skin);
			float sliderMax = 100;
			Slider slider = new Slider(0f, sliderMax, 1f, false, skin);
			slider.setValue(sliderMax / 2);
			slider.addListener(new ChangeListener() {
				public void changed(ChangeListener.ChangeEvent ev, Actor a) {
					float progress = slider.getValue();
					float scl = progress / sliderMax * 2;
//					worldWidth = scl * defWorldWidth;
//					worldHeight = scl * defWorldHeight;
					label.setText(String.format("子视口拉伸(%.2f)", scl));
				}
			});
			ajustChildViewportScl.add(label);
			ajustChildViewportScl.add(slider).expandX().fillX();
			modifierTable.add(ajustChildViewportScl).expandX().fillX().row();
		}

		//包裹器
		ScrollPane modifierVerti = new ScrollPane(modifierTable);
		modifierVerti.setScrollingDisabled(true, false);

		//分割板
		GSplitPane modifierSplit = new GSplitPane(modifierVerti, gameTable, false, skin);
		modifierSplit.setToggleData(0, 0.3f);
		modifierSplit.toggle(true);

//		Table consolePane = new Table(skin);
//		consolePane.setFillParent(true);
//		consolePane.top().left();
//		consolePane.add(logViewer.getRootLayout()).expand().fill();

		GSplitPane testSplit = new GSplitPane(modifierSplit, logViewer.getRootLayout(), true, skin);
		testSplit.setFillParent(true);
		testSplit.setToggleData(1, 0.8f);
		testSplit.toggle(true);

		uiStage.addActor(testSplit);
	}


	@Override
	public void render(float delta) {
		ScreenUtils.clear(0.4f, 0.4f, 0.4f, 1);

		//绘制背景
		Batch batch = uiStage.getBatch();
		batch.begin();
		batch.draw(backTex, 0, 0, editorWidth, editorHeight);
		batch.end();

		uiStage.act(delta);
		uiStage.draw();
	}

	@Override
	public void resize(int width, int height) {
		super.resize(width, height);
		editorWidth = editorViewport.getWorldWidth();
		editorHeight = editorViewport.getWorldHeight();
	}


	public static class InnerViewWidgetGroup extends WidgetGroup {
		private final Viewport editorViewport;
		private float worldWidth = 320, worldHeight = 240;
		private final OrthographicCamera worldCam;
		private Image avatarImage;
		private Skin skin;
		private Texture worldBackTex, avatarTex;
		private Vector2 screenPos = new Vector2();

		public InnerViewWidgetGroup(Skin skin, Viewport editorViewport) {
			this.skin = skin;
			this.editorViewport = editorViewport;
			worldBackTex = ColorTextureUtils.createColorTexture(Color.BLACK);
			avatarTex = GlobalAssets.getInstance().avatarTex;

			worldCam = new OrthographicCamera();

			avatarImage = new Image(avatarTex);
			addActor(avatarImage);
		}

		@Override
		public void act(float delta) {
			super.act(delta);
			//local转screen坐标
			{
				screenPos.set(getX(), getY());
				localToScreenCoordinates(screenPos);
				LogViewerService.logDebug("getX: %.1f, getY: %.1f, screenX: %.1f, screenY: %.1f, width: %.1f, height: %.1f", getX(), getY(), screenPos.x, screenPos.y, getWidth(), getHeight());
			}
			avatarImage.setSize(worldWidth / 2, worldHeight / 2);
			//avatarImage.setPosition(getX(), getY());
		}

		@Override
		public void draw(Batch batch, float parentAlpha) {
			Gdx.gl.glViewport((int) screenPos.x, (int) screenPos.y, 100, 100);
			updateWorldCam();
			batch.setProjectionMatrix(worldCam.combined);
			super.draw(batch, parentAlpha);
			batch.setProjectionMatrix(editorViewport.getCamera().combined);
			editorViewport.apply(true);

			//ui部分采用原始绘制
		}

		private void updateWorldCam() {
			float width = getWidth();
			float height = getHeight();

			worldCam.viewportWidth = width;
			worldCam.viewportHeight = height;
			worldCam.position.set(width / 2, height / 2, 0);
			worldCam.update();
		}
	}

}
